package com.eqdd.yiqidian.ui.yiqixue.add;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.ParseVideoBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditIconBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.select.SelectTwoDeepMultiTagActivity;
import com.eqdd.yiqidian.utils.HttpUtil;
import com.eqdd.yiqidian.utils.SplitUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.List;

/**
 * @author吕志豪 .
 * @date 18-2-25  上午9:26.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class AddVideoActivity extends SlimBaseMultiEditActivity {

    private RecyclerViewCustom dataBinding;
    private ParseVideoBean parseVideoBean;
    private SlimEditEnterBean linkBean;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "发布视频");
        initTopRightText("发布", v -> {
            if (checkParams()) {
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.LEARN_ADD_VIDEO)
                        .params("userGuid", AppConstent.user.getGuid())
                        .params("videoUrl", setParam(0))
                        .params("lable", setParam(3))
                        .params("describe", setParam(4))
                        .params("player", parseVideoBean.getPlayer())
                        .params("id", parseVideoBean.getId())
                        .params("title", setParam(1))
                        .params("bigThumbnail", setParam(2))
                        .params("videoTime", parseVideoBean.getDuration())
                        .execute(new DialogCallBack<HttpResult>(AddVideoActivity.this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    setResult(Config.SUCCESS);
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort(msg);
            }
        });
    }

    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (slimAdapterEx.getDataItem(p).getPosition()) {
            case 3:
                Bundle bundle = new Bundle();
                bundle.putInt(Config.OPTION, 45);
                bundle.putInt(Config.MAX_NUM, 5);
                startActivityForResult(SelectTwoDeepMultiTagActivity.class, bundle, Config.SELECT_TWO_DEEP_MULTI_TAG);
                break;
        }
    }

    @Override
    protected void setEditBeans() {
        isRead = false;
        isSave = false;
        linkBean = (SlimEditEnterBean) new SlimEditEnterBean("*视频链接", "", 0, "请输入视频地址").setTip("支持优酷视频，请将视频网页地址粘贴到下面的输入框中").setNeed(true);
        datas.add(linkBean);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Config.SUCCESS) {
            switch (requestCode) {
                case Config.SELECT_TWO_DEEP_MULTI_TAG:
                    List tags = data.getStringArrayListExtra(Config.STRING_LIST);
                    String combin = SplitUtil.combin(tags, ",");
                    slimAdapterEx.getDataItem(3).setContent(combin);
                    slimAdapterEx.notifyItemChanged(3);
                    break;
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == 0) {
                String content = data.getStringExtra(Config.CONTENT);
                HttpUtil.parseVideo(content, object -> {
                    if (object == null) {
                        ToastUtil.showShort("视频解析失败");
                    } else {
                        parseVideoBean = (ParseVideoBean) object;
                        int removeSize = datas.size();
                        datas.clear();
                        slimAdapterEx.notifyItemRangeRemoved(0, removeSize);
                        datas.add(linkBean);
                        datas.add(new SlimEditEnterBean("*视频标题", parseVideoBean.getTitle(), 1, "请输入视频标题").setNeed(true));
                        datas.add(new SlimEditIconBean("*视频封面", parseVideoBean.getThumbnail(), 2).setNeed(true));
                        datas.add(new SlimEditEnterBean("*视频分类", "", 3, true, "最多选5个").setNeed(true));
                        datas.add(new SlimEditEnterBean("*视频描述", parseVideoBean.getDescription(), 4, "请输入视频描述").setNeed(true));
                        slimAdapterEx.notifyItemRangeInserted(0, 5);

                    }
                });

            }
        }
    }
}
